package com.org.web.handle.classify.controller;

import com.org.platform.model.ResultModel;
import com.org.platform.utils.CommonUtil;
import com.org.web.handle.classify.service.ClassifyService;
import com.org.web.support.base.BaseController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author devg @Date 2020/2/28 10:57
 */
@RestController
@RequestMapping(value = {"/classify"})
public class ClassifyController extends BaseController {

  @Autowired
  private ClassifyService classifyService;

  @RequestMapping(value = {"/index"})
  public ModelAndView index() {
    Long defaultClassifyId = null;
    Map<String, Object> param = new HashMap<>();
    param.put("classifyLevel", 1);
    List<Map<String, Object>> classifyList = classifyService.getClassifyList(param);
    if (!CommonUtil.isNullOrEmpty(classifyList)) {
      defaultClassifyId = CommonUtil.getMapLong(classifyList.get(0), "classifyId");
    }
    Map<String, Object> model = new HashMap<>();
    model.put("footNav", "classify");
    model.put("classifyList", classifyList);
    model.put("defaultClassifyId", defaultClassifyId);
    return modelAndView("/front/classify", model);
  }

  @RequestMapping(value = {"/getClassify"})
  public ResultModel getClassify(@RequestBody Map<String, Object> param) {
    List<Map<String, Object>> classifyList = classifyService.getClassifyList(param);
    Map<String, Object> result = new HashMap<>();
    result.put("classifyList", classifyList);
    return ResultModel.newSuccess(result);
  }

}
